<?php
/**
 * Created by PhpStorm.
 * User: 我想有个家
 * Date: 2019/8/31
 * Time: 14:27
 */

namespace App\Controller\admin;


use App\Model\User;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;

/**
 * Class User
 * @package App\Controller\admin
 * @Controller(prefix="a/user")
 * @Middleware(\App\Middleware\AdminMiddleware::class)
 */
class UserController extends AdminBase {

    /**
     * 用户列表
     * @return array
     * @RequestMapping(path="lists",methods="get")
     */
    public function getLists() {
        $size = $this->get('size');
        $status = $this->get('status');
        $time = (string) $this->get('time');
        $size = pageSizeVerify($size);
        $condition = [];
        $time = explode(',', $time);
        if (count($time) == 2) {
            $time[0] = $time[0] . ' 00:00:00';
            $time[1] = $time[1] . ' 23:59:59';
            $condition[] = ['create_time', 'between', $time];
        }
        switch ($status) {
            case 'yes':
                $condition[] = ['status', '=', 0];
                break;
            case 'no':
                $condition[] = ['status', '=', 1];
                break;
        }
        $result = User::query()->where($condition)
            ->select('id', 'mobile', 'nick_name', 'status', 'create_time')
            ->paginate($size);
        return success(paginate($result));
    }

    /**
     * 禁用、启用账号
     * @return array
     * @RequestMapping(path="disable",methods="put")
     */
    public function putDisable() {
        $uid = $this->body('uid');
        $status = $this->body('status');
        if ($status != 0) {
            $status = 1;
        }
        User::query()->where('id', $uid)->update(['status' => $status]);
        return success();
    }
}
